<?php


namespace app\manage\controller;

use app\common\model\Goods AS GoodsModel;
use app\common\model\GoodSku AS GoodSkuModel;
use app\common\model\RoleGoods AS RoleGoodsModel;
use app\common\model\MemberRoles AS MemberRolesModel;
use app\common\model\RoleGoodsSku AS RoleGoodsSkuModel;

class RoleGoods extends Base
{
    public function rlist()
    {
        $id =input('id');
        // 当前页数
        $page   = input('get.page/d', 1);
        $goods_name=input('goods_name','');

        // 分页条件
        $offset = $this->offset($page, 15);


        //end
        $where[]=[
            ['is_sale','eq',1],
            ['is_role','eq',1]
        ];

        if($goods_name){
            $where[]=[
                ['goods_name','like','%'.$goods_name.'%']
            ];
        }
        // 数据总数
        $count  = GoodsModel::where($where)->count();

        $list=GoodsModel::where($where)->field('id,goods_name,market_price,sale_price,goods_stock')->order('id desc')->limit($offset)->select()->toArray();

        // 模板
        $template = $this->indexTemplate();

        $this->assign('act_id',$id);
        $this->assign('goods_name',$goods_name);
        return $this->fetch($template, [
            'page'  => $page,
            'count' => $count,
            'limit' => 15,
            'list'  => $list,
        ]);
    }

    /**
     * 设置角色价格
     */
    public function roleadd(){
        $id =input('id');

        $info=GoodsModel::where('id',$id)->field('id,goods_name,sale_price')->find();

        //获取角色
        $mr_list=MemberRolesModel::field('id,name,0 sale_price')->select()->toArray();
        if($mr_list){
           $role_ids= array_column($mr_list,'id');

           $r_where=[
               ['goods_id','eq',$id],
               ['role_id','in',$role_ids]
           ];
           $r_list=RoleGoodsModel::where($r_where)->field('id,role_id,goods_id,sale_price')->select()->toArray();
           if($r_list){
               foreach ($mr_list as $k=>$v){
                   foreach ($r_list as $rv){
                        if($v['id']==$rv['role_id']){
                            $mr_list[$k]['sale_price']=$rv['sale_price'];
                        }
                   }
               }
           }
        }

        $this->assign('mr_list',$mr_list);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 获取活动商品
     */
    public function getgoods(){
        $id =input('id');
        // 当前页数
        $page   = input('get.page/d', 1);
        $goods_name=input('goods_name','');

        // 分页条件
        $offset = $this->offset($page, 15);

        //end
        $where[]=[
            ['is_sale','eq',1],
            ['is_role','eq',0]
        ];

        if($goods_name){
            $where[]=[
                ['goods_name','like','%'.$goods_name.'%']
            ];
        }
        // 数据总数
        $count  = GoodsModel::where($where)->count();


        $list=GoodsModel::where($where)->field('id,goods_name,market_price,sale_price,goods_stock')->order('id desc')->limit($offset)->select()->toArray();

        // 模板
        $template = $this->indexTemplate();

        $this->assign('act_id',$id);
        $this->assign('goods_name',$goods_name);
        return $this->fetch($template, [
            'page'  => $page,
            'count' => $count,
            'limit' => 15,
            'list'  => $list,
        ]);
    }

    /**
     * 设置规格价格
     */
    public function addsku(){

        $goods_id =input('goods_id',0);
        $role_id   = input('role_id', 0);

        $where[]=[
            ['goods_id','eq',$goods_id],
        ];
       $sku_list= GoodSkuModel::where($where)->field('id,goods_id,sku_name,sale_price,0 rsku_price,stock')->select()->toArray();

       $where[]=[['role_id','eq',$role_id]];
       $rs_list= RoleGoodsSkuModel::where($where)->field('id,goods_sku_id,sale_price')->select();
       foreach ($sku_list as $k=>$v){
           foreach ($rs_list as $sv){
               if($v['id']==$sv['goods_sku_id']){
                   $sku_list[$k]['rsku_price']=$sv['sale_price'];
               }
           }
       }

       $this->assign('goods_id',$goods_id);
       $this->assign('role_id',$role_id);
       $this->assign('sku_list',$sku_list);

       return $this->fetch();
    }

    /**
     * 数据保存
     */
    public function saveGoods(){
        $goods_ids=input('goods_id','');


        $a_save=[
            'id'=>$goods_ids,
            'is_role'=>1,
        ];

        $res=(new GoodsModel())->isUpdate(true)->save($a_save);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }

    /**
     * 设置活动价
     */
    public function setPrice(){
        $role_id=input('role_id',0);
        $goods_id=input('goods_id',0);
        $sale_price=input('sale_price','');

        $where=[
            ['role_id','eq',$role_id],
            ['goods_id','eq',$goods_id]
        ];
        $rg_goods_id=RoleGoodsModel::where($where)->value('id');

        $save['goods_id']=$goods_id;
        $save['role_id']=$role_id;
        $save['sale_price']=$sale_price;

        $isUpadate=false;
        if($rg_goods_id){
            $save['id']=$rg_goods_id;
            $isUpadate=true;
        }

        $res=(new RoleGoodsModel())->isUpdate($isUpadate)->save($save);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }

    /**
     * 角色规格价格
     */
    public function setSkuPrice(){
        $role_id=input('role_id',0);
        $goods_id=input('goods_id',0);
        $sale_price=input('sale_price',0);
        $goods_sku_id=input('sku_id',0);

        $where=[
            ['role_id','eq',$role_id],
            ['goods_id','eq',$goods_id],
            ['goods_sku_id','eq',$goods_sku_id],
        ];
        $id=RoleGoodsSkuModel::where($where)->value('id');

        $save['goods_id']=$goods_id;
        $save['role_id']=$role_id;
        $save['sale_price']=$sale_price;
        $save['goods_sku_id']=$goods_sku_id;

        $isUpadate=false;
        if($id){
            $save['id']=$id;
            $isUpadate=true;
        }

        $res=(new RoleGoodsSkuModel())->isUpdate($isUpadate)->save($save);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }

    /**
     * 删除角色
     */
    public function delRoleDel(){

        $goods_id =input('goods_id');

        $res=(new GoodsModel())->where('id',$goods_id)->update(['is_role'=>0]);
        if($res){

            (new RoleGoodsModel())->where('goods_id',$goods_id)->update(['delete_time'=>time()]);
            (new RoleGoodsSkuModel())->where('goods_id',$goods_id)->update(['delete_time'=>time()]);
        }

        $data=['code'=>-1,'msg'=>'删除失败'];
        if($res){
            $data=['code'=>1,'msg'=>'删除成功'];
        }

        return $data;
    }
}